package com.wgchao.pharmacymg.domain.event;

import com.wgchao.pharmacymg.application.mapstruct.dto.PhDrugStorageDTO;
import com.wgchao.pharmacymg.application.mapstruct.mapper.PhDrugStorageMapper;
import com.wgchao.pharmacymg.domain.dao.PhDrugStorageDao;
import com.wgchao.pharmacymg.domain.entity.PhDrugStorageDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: DrugStorageEvent
 * @author: wgchao
 * @createTime: 2019/11/17 10:09 AM
 * @Description: 药库事件
 */
@Service
public class DrugStorageEvent {
    @Autowired
    private PhDrugStorageDao drugStorageDao;
    @Autowired
    private PhDrugStorageMapper drugStorageMapper;

    /**
     * 新增药库
     * @param drugStorageDTO
     * @return
     */
    public PhDrugStorageDTO save(PhDrugStorageDTO drugStorageDTO){
        PhDrugStorageDO phDrugStorageDO = drugStorageMapper.toEntity(drugStorageDTO);
        phDrugStorageDO.save();
        phDrugStorageDO = drugStorageDao.save(phDrugStorageDO);
        return drugStorageMapper.toDTO(phDrugStorageDO);
    }

    /**
     * 更新药库信息
     * @param drugStorageDTO
     * @return
     */
    public PhDrugStorageDTO update(PhDrugStorageDTO drugStorageDTO){
        PhDrugStorageDO phDrugStorageDO = drugStorageMapper.toEntity(drugStorageDTO);
        phDrugStorageDO = drugStorageDao.save(phDrugStorageDO);
        return drugStorageMapper.toDTO(phDrugStorageDO);
    }

    /**
     * 删除药库
     * @param storageCode 药库编号
     */
    public void delete(String storageCode){
        PhDrugStorageDO drugStorageDO = drugStorageDao.findByCode(storageCode);
        drugStorageDO.delete();
        drugStorageDO = drugStorageDao.save(drugStorageDO);
    }

    /**
     * 查询药库信息
     * @param storageCode
     * @return
     */
    public PhDrugStorageDTO findStorageInfo(String storageCode){
        PhDrugStorageDO drugStorageDO = drugStorageDao.findByCode(storageCode);
        return drugStorageMapper.toDTO(drugStorageDO);
    }

    /**
     * 条件查询药库信息
     * @param storageCode 药库编码
     * @param agencyCode 机构编码
     * @param storageName 药库名称
     * @param dsl 药库种类
     * @return
     */
    public List<PhDrugStorageDTO> qbc(String storageCode, String agencyCode, String storageName, Long dsl){
        List<PhDrugStorageDO> drugStorageDOS = drugStorageDao.qbc(storageCode, agencyCode, storageName, dsl);
        return drugStorageMapper.toDTO(drugStorageDOS);
    }
}
